<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <div class="logo">
          <LogoTitle title="懒熊文档" size="large" :show-logo="true" />
          <p class="logo-subtitle">SlothBear Documentation System</p>
        </div>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        size="large"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="login-button"
            :loading="loading"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>

        <div class="login-tips">
          <p class="tip-text">
            没有账号？<span class="link-text" @click="showRegisterTip">立即注册</span>
          </p>
          <p class="tip-text">
            <span class="link-text" @click="continueWithoutLogin">不登录，继续浏览</span>
          </p>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import LogoTitle from '@/components/LogoTitle.vue'
import { useUserStore } from '@/shared/user.ts'

const router = useRouter()
const userStore = useUserStore()

// 表单ref
const loginFormRef = ref<FormInstance>()
const loading = ref(false)

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    const success = await userStore.login({
      username: loginForm.username,
      password: loginForm.password
    })

    if (success) {
      // 登录成功，跳转到首页或之前的页面
      const redirect = router.currentRoute.value.query.redirect as string
      router.push(redirect || '/apps')
    }
  } catch (error) {
    console.error('表单验证失败:', error)
  } finally {
    loading.value = false
  }
}

// 显示注册提示
const showRegisterTip = () => {
  ElMessageBox.alert(
    '注册功能正在开发中，请联系管理员开通账号。',
    '提示',
    {
      confirmButtonText: '我知道了',
      type: 'info'
    }
  )
}

// 不登录继续浏览
const continueWithoutLogin = () => {
  ElMessage.info('游客模式，部分功能受限')
  router.push('/apps')
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--el-bg-color);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px 32px;
  backdrop-filter: blur(10px);
  border: 1px solid var(--el-border-color-light);
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}


.logo-subtitle {
  font-size: 14px;
  color: var(--el-text-color-regular);
  margin: 0;
  opacity: 0.8;
}

.login-form {
  width: 100%;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
}

.login-tips {
  margin-top: 20px;
  text-align: center;
}

.tip-text {
  margin: 8px 0;
  font-size: 14px;
  color: var(--el-text-color-regular);
}

.link-text {
  color: var(--el-color-primary);
  cursor: pointer;
  text-decoration: none;
  transition: color 0.2s;
}

.link-text:hover {
  color: var(--el-color-primary-light-3);
}

/* 深色模式适配 */
.dark .login-container {
  background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
}

.dark .login-card {
  background: var(--el-bg-color-page);
  border: 1px solid var(--el-border-color);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    padding: 20px 16px;
  }
  
  .login-card {
    padding: 32px 24px;
  }
  
  .logo-text {
    font-size: 24px;
  }
}
</style>